<template>
  <div>
    <div class="ccui-multifunctional-table-wrapper">
      <el-table
        align="center"
        ref="multipleTable"
        :data="infoData"
        :isshow="true"
        border
        max-height="9999"
        class="ccui-multifunctional-table"
        :header-cell-class-name="must"
        :summary-method="getSummariesInfo"
        lazy
        show-summary
      >
        <el-table-column type="index" label="序号" width="80" fixed="left" />
        <template v-for="(table, index) in tableColumnInfo">
          <el-table-column
            :key="index"
            :prop="table.prop"
            :label="table.label"
            :fixed="table.fixed"
            :width="table.width"
            :align="table.align"
            :header-align="table.headAlign"
            show-overflow-tooltip
          />
        </template>
      </el-table>
    </div>
  </div>
</template>

<script>
// import { formatNumber } from "@/basa/utils";
export default {
  props: {
    tableInfo: Array
  },
  data() {
    return {
      //   tableData: [],
      infoData: [],
      tableColumnInfo: [
        {
          prop: "invoiceTypeName",
          label: "发票类型",
          fixed: "left",
          width: "100"
        },
        {
          prop: "invoiceNo",
          label: "发票号码",
          fixed: "left",
          width: "130"
        },
        {
          prop: "invoiceCode",
          label: "发票代码",
          width: "130"
        },
        // {
        //   prop: "ratePercent",
        //   label: "税率",
        //   width: "100",
        //   align: "right",
        //   headAlign: "left"
        // },
        {
          prop: "priceTaxTotal",
          label: "价税合计",
          width: "130",
          align: "right",
          headAlign: "left"
        },
        {
          prop: "tax",
          label: "税额",
          width: "130",
          align: "right",
          headAlign: "left"
        },
        {
          prop: "amountExcludeTax",
          label: "不含税金额",
          width: "130",
          align: "right",
          headAlign: "left"
        },
        {
          prop: "invoiceDate",
          label: "开票日期",
          width: "150",
          headAlign: "left"
        },
          {
              prop: "seller",
              label: "销售方名称",
              width: "150",
              headAlign: "left",
              align:"left"
          },
          {
              prop: "purchaser",
              label: "购买方名称",
              width: "150",
              headAlign: "left",
              align:"left"
          },
        {
          prop: "remake",
          label: "备注",
          width: "100",
          headAlign: "left"
        }
      ]
    };
  },
  methods: {
    // 发票信息
    getSummariesInfo(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        if (
          [
            "priceTaxTotal",
            "deductionRate",
            "amountExcludeTax",
            "tax",
            "deductionTax"
          ].includes(column.property)
        ) {
          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                let num = Number(prev) + Number(curr);
                return num;
              } else {
                return Number(prev);
              }
            }, 0);
            if (
              index == 5 ||
              index == 6 ||
              index == 7 ||
              index == 8 ||
              index == 9
            ) {
              sums[index] = Number(sums[index]).toFixed(2);
              // sums[index] = formatNumber(Number(sums[index]),2);
            }
          }
        }
      });
      // 价税合计=本次收票金额（基本信息）
      sums.forEach((e, i) => {
        if (i == 5) {
          this.$bus.$emit("priceTaxTotalSum", e);
        }
      });
      return sums;
    },
    must(obj) {
      if (
        obj.columnIndex == 0 ||
        obj.columnIndex == 9 ||
        obj.columnIndex == 10
      ) {
        return;
      } else {
        return "must";
      }
    }
  },
  activated() {
    this.$bus.$on("getTableList", Message => {
      // console.log(Message);
      this.tableData = Message.invoceInfo;
      this.tableData.forEach(v => {
        v.priceTaxTotal = Number(v.priceTaxTotal).toFixed(2);
        v.rate = new this.$BigNumber(Number(v.rate)).multipliedBy(100) + "%";
        v.tax = Number(v.tax).toFixed(2);
        v.amountExcludeTax = Number(v.amountExcludeTax).toFixed(2);
        v.invoiceDate = v.invoiceDate.substring(0, 10);
      });
    });
  },
  watch: {
    tableInfo: {
      handler(n) {
        this.infoData = n;
        this.infoData.forEach(v => {
          v.priceTaxTotal = Number(v.priceTaxTotal).toFixed(2);
          v.rate = new this.$BigNumber(Number(v.rate)).multipliedBy(100) + "%";
          v.tax = Number(v.tax).toFixed(2);
          v.amountExcludeTax = Number(v.amountExcludeTax).toFixed(2);
          v.invoiceDate = v.invoiceDate.substring(0, 10);
        });
      }
    }
  }
};
</script>

<style lang="scss" scoped>
// /deep/ .el-table th.must > .cell:before {
//   content: "* ";
//   color: #ff1818;
// }
/deep/ .el-table__fixed-header-wrapper {
  th {
    pointer-events: auto;
  }
}

/deep/ .el-table__fixed,
/deep/.el-table__fixed-right {
  pointer-events: none;
  td {
    pointer-events: auto;
  }
}
</style>
